package com.utilviet.baovn.objects;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.utilviet.baovn.R;

/**
 * Created by leanhtai on 6/20/15.
 */
public class CustomWebView extends RelativeLayout {

    private ProgressBar mProgressBar = null;
    private View mProgressShadow = null;
    public BaseWebView mWebView = null;
    public ICustomWebView mICustomWebView = null;


    public String mUrl = null;

    public CustomWebView(Context context) {
        super(context);
        initialize();
    }

    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    public void loadUrl(String url) {
        mUrl = url;

        if (mWebView != null && mUrl != null) {
            mWebView.loadUrl(mUrl);
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mProgressBar = (ProgressBar) findViewById(R.id.progress);
        mProgressShadow = (View) findViewById(R.id.progressShadow);

        //
        mWebView = (BaseWebView) findViewById(R.id.webView);
        mWebView .getSettings().setJavaScriptEnabled(true);
        mWebView .clearCache(true);

        mWebView .getSettings().setSupportZoom(true);
        mWebView .getSettings().setBuiltInZoomControls(true);
        mWebView .getSettings().setDisplayZoomControls(true);
        mWebView .getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 4.0; en-gb; Nexus One Build/FRF50) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");

        //
        mWebView .setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);

                if (mICustomWebView != null) {
                    mICustomWebView.customWebViewDidClickLink(CustomWebView.this, url);
                }
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                System.gc();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mProgressBar.setVisibility(View.INVISIBLE);

                super.onReceivedError(view, errorCode, description, failingUrl);

                System.gc();
            }
        });

        //
        mWebView.setWebChromeClient( new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                if (progress < 100) {
                    mProgressBar.setVisibility(ProgressBar.VISIBLE);
                    mProgressShadow.setVisibility(View.VISIBLE);
                }

                mProgressBar.setProgress(progress);

                if (progress == 100) {
                    mProgressBar.setVisibility(ProgressBar.INVISIBLE);
                    mProgressShadow.setVisibility(View.INVISIBLE);
                }
            }
        });

        mWebView.mIWebView = new IBaseWebView() {
            @Override
            public void webViewDidLongPress(BaseWebView webView) {
                if (mICustomWebView != null) {
                    mICustomWebView.customWebViewWillShare(CustomWebView.this, mUrl);
                }
            }
        };

        loadUrl(mUrl);
    }

    private void initialize() {
    }
}
